import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:learnfluttersecond/res.dart';

class SearchBar extends StatefulWidget {
  ///边距 左上右下
  final double marginLeft;
  final double marginTop;
  final double marginRight;
  final double marginBottom;

  ///背景色
  final Color bgCorlor;

  ///背景圆角大小
  final double borderRadius;

  ///未输入内容时的提示文字
  final String hintText;

  SearchBar(
      {Key key,
      this.marginLeft = 0.0,
      this.marginRight = 0.0,
      this.marginTop = 0,
      this.marginBottom = 0,
      this.hintText = "",
      this.bgCorlor = const Color(0xFFF5F6FA),
      this.borderRadius = 5.0})
      : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: widget.bgCorlor,
          borderRadius: BorderRadius.circular(widget.borderRadius)),
      margin: EdgeInsets.only(
          left: widget.marginLeft,
          right: widget.marginRight,
          top: widget.marginTop,
          bottom: widget.marginBottom),
      padding: EdgeInsets.only(left: 15),
      height: 38,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
              child: TextField(
            minLines: 1,
            textAlignVertical: TextAlignVertical.center,
            textAlign: TextAlign.left,
            style: TextStyle(textBaseline: TextBaseline.alphabetic),
            decoration: InputDecoration(
                alignLabelWithHint: true,
                isDense: true,
                icon: Image.asset(Res.sousouos, width: 13.5, height: 13.5),
                border: InputBorder.none,
                hintText: widget.hintText,
                hintStyle: TextStyle(color: Color(0xFFA3A9BB), fontSize: 13)),
            textInputAction: TextInputAction.search,
            inputFormatters: [
              LengthLimitingTextInputFormatter(20),
            ],
            keyboardType: TextInputType.text,
          )),
          SizedBox(
            width: 10,
          )
        ],
      ),
    );
  }
}
